#
# ship.py -- rendering ships
# 
# author: Tom "Knio" Flanagan
#         knio@h4xn3t.net
#         theknio@gmail.com
#         irc.freenode.net - Knio - #python #pygame #py
#
# this file released under Public Domain
#

from constants import *
import object
import weapon

class Ship(object.Object):
    def init(self):
        self.sprites = {}
        self.image = self.make()
        for i in self.model.weapons:
            i.view = weapon.Weapon(self.view, i)

    def make(self):
        image = pygame.image.load(self.model.sprite)
        array = pygame.surfarray.array3d(image)
        for i in array:
            for n in i:
                if n.tolist() == BLACK: continue
                for x in [R,G,B]:
                    n[x] = int(n[x] * (self.color[x]/255.0))
        image = pygame.surfarray.make_surface(array)
        image.set_colorkey((0,0,0,0), RLEACCEL)
        size = image.get_size()
        size = (int(size[0] * (self.model.size/15.)), int(size[1] * (self.model.size/15.)))
        image = pygame.transform.scale(image, size)
        return image.convert()

    def get_sprite(self, angle):
        angle = int(angle)
        if not angle in self.sprites:
            self.sprites[angle] = pygame.transform.rotate(self.image, angle)
        return self.sprites[angle]


    def tick(self):
        self.view.dirty.append(pygame.Rect(self.rect))
        self.sprite = self.get_sprite(self.model.f/PI*180-90)
        self.rect = self.sprite.get_rect()
        self.rect.centerx = self.view.offset+self.model.x
        self.rect.centery = self.view.offset-self.model.y
        self.view.dirty.append(pygame.Rect(self.rect))
        self.render()
        for i in self.model.weapons:
            i.view.tick()

    def render(self):
        self.view.screen.blit(self.sprite, self.rect)



